package com.ftwj.demo.utils.validate;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.springframework.util.StringUtils;

public class PhoneValidation implements ConstraintValidator<Phone, String>{
	
	private Boolean required=false;
	
	@Override
	public void initialize(Phone constraintAnnotation) {
		required = constraintAnnotation.required();
	}

	@Override
	public boolean isValid(String value, ConstraintValidatorContext context) {
		if(required) {
			return ValidateUtil.isMobile(value);
		}else {
			if(StringUtils.isEmpty(value)) {
				return false;
			}
			return ValidateUtil.isMobile(value);
		}
	}
	
}
